import React from 'react';
import { useDispatch } from 'react-redux';
import { AppDispatch } from '../../../store';
import { register } from '../../../actions/userActions';
import AuthForm from '../AuthForm';

const RegisterForm: React.FC = () => {
  const dispatch = useDispatch<AppDispatch>();
  
  const onFinish = async (values: { username: string; password: string; email?: string }) => {
    if (!values.email) throw new Error('Email is required');
    try {
      await dispatch(register(values));
      // 处理成功后的逻辑，例如重定向
    } catch (err) {
      console.error('Registration failed:', err);
      // 可以显示错误信息给用户
    }
  };

  return <AuthForm onSubmit={onFinish} isRegister />;
};

export default RegisterForm;